package ru.urfc.portal.ui.client.renderers;

import ru.urfc.portal.ui.client.descriptor.GridField;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;

public class ExtSelectFieldRenderer extends ExtFieldRenderer {

	protected ComboBox<ModelData> extField;
	
	public ExtSelectFieldRenderer(GridField gridField, ComboBox<ModelData> extField) {
		super(gridField, extField);
		this.extField = extField;
	}

	public ComboBox<ModelData> getCombobox() {
		return extField;
	}
	
	@Override
	public void renderElements(ModelData modelData) {
		ListStore<ModelData> store = extField.getStore();
		Object value = field.getValue(modelData);
		for (int i = 0; i < store.getCount(); ++i) {
			Object storeItemValue = store.getAt(i).get(extField.getValueField());
			if (storeItemValue.equals(value)) {
				extField.setValue(store.getAt(i));
			}
		}
	}

	@Override
	public void updateModel(ModelData modelData) {
		ModelData selectedModelData = extField.getValue();
		if (selectedModelData != null) {
			field.setValue(modelData, selectedModelData.get(extField.getValueField()));
		}
	}
	
}
